var loginVue = new Vue({
	el:"#app",
	data:{
		btn_text:"发送验证码",
		tel:"",
		code:"", 
		isAgreements: !1, 
		isLocalAgree: !1,
		isDisabled: !0,
		isCheck: !1,
		isNeedLogin: !0
	},
	methods:{
		sendCode:function(){
			if(this.btn_text!="发送验证码"){
				return;
			}
			if(!this.tel){
				return;
			}
			if(!(/^1(3|4|5|7|8)\d{9}$/.test(this.tel))){ 
				wx.showToast({title:"手机号码有误!"});
				return;
			}
			wx.showLoading({title:"加载中..."});
			this.$http.post(baseUrl+"code/send",{
				mobile:this.tel
			}).then(function(e){
				var d = e.data;
				if(d.success){
					this.second();
					wx.hideLoading(), wx.showToast({
						title: "发送成功!注意查收",
						icon: "none",
						duration: 2e3,
						mask: !0
					});
				}else{
					wx.hideLoading(), wx.showToast({
						title: d.description,
						icon: "none",
						duration: 2e3,
						mask: !0
					});
				}
			}).catch(function(e){
				wx.hideLoading(), wx.showToast({
					title: "网络错误，请稍后重试",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			});
		},
		second:function(){
			var i = 60;
			var t = this;
			var interval = setInterval(function(){
				if(i==0){
					clearInterval(interval);
					t.btn_text="发送验证码";
					return;
				}
				t.btn_text = i-- + '秒后可重新发送';
			},1000)
		},
		userLogin:function(){
			const that = this;
			if(!that.isLocalAgree){
				wx.showToast({title:"请先同意美匠子用户注册协议!"});
				return;
			}
			if(!this.tel){
				return;
			}
			if(!(/^1(3|4|5|7|8)\d{9}$/.test(this.tel))){ 
				wx.showToast({title:"手机号码有误!"});
				return;
			}
			if(!this.code){
				wx.showToast({title:"请输入验证码!"});
				return;
			}
			if(this.code.length!=6){
				wx.showToast({title:"请正确输入验证码!"});
				return;
			}
			wx.showLoading({title:"加载中..."});
			const encryptCode = sha256_digest(this.code);
			this.$http.post(baseUrl+"app/login",{
				tel:this.tel,
				code:encryptCode
			}).then(function(e){
				wx.hideLoading();
				var d =e.data;
				if(!d.success){
					wx.showToast({
						title: d.description
					});
					localStorage.removeItem("open_id");
					localStorage.removeItem("userId");
					localStorage.removeItem("user_data");
					return;
				}
				localStorage.setItem("isAgreements", that.isLocalAgree);
				localStorage.setItem("_crsf", encryptCode);
				localStorage.setItem("userTel", that.tel); 
				localStorage.setItem("open_id",d.data.openid);
				localStorage.setItem("userId",d.data.userId);
				localStorage.setItem("user_data",JSON.stringify(d.data));
				that.userAgree();
				login._back();
			}).catch(function(e){
				wx.hideLoading(), wx.showToast({
					title: "网络错误，请稍后重试",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			})
			
		}, 
		comfireBtn: function(){
			this.isCheck ? (this.isCheck = !this.isCheck, this.isDisabled = !0, this.isLocalAgree = !1) : (this.isCheck = !0, 
			this.isDisabled = !1, this.isLocalAgree = !0);
		}, 
		toAgreements: function(){
			Jump.to(page.mine.agreements);
		},
		userAgree: function(){
			this.$http.post(baseUrl+"api/sign/userAgree",{
			}).then(function(e){
				var d =e.data;
				if(!d.success){
					console.log("更新用户同意状态失败:"+d.description);
				}else{
					console.log("更新用户同意状态成功");
				}
			}).catch(function(e){
				console.log("更新用户同意状态失败:"+e);
			})
		}, 
		login2: function(tel, code){
			wx.showLoading({title:"跳转中..."});
			this.$http.post(baseUrl+"app/login",{
				tel:tel,
				code:code
			}).then(function(e){
				wx.hideLoading();
				var d =e.data;
				if(!d.success){
					this.isNeedLogin = !0; 
					localStorage.removeItem("open_id");
					localStorage.removeItem("user_data");
					return;
				}
				localStorage.setItem("open_id",d.data.openid);
				localStorage.setItem("userId",d.data.userId);
				localStorage.setItem("user_data",JSON.stringify(d.data));
				login._back();
			}).catch(function(e){
				wx.hideLoading();
			})
		}
	},
	created:function(){
		const isAgreements = localStorage.getItem("isAgreements");
		this.isAgreements = isAgreements && isAgreements == 'true' ? true : false;
		this.isLocalAgree = this.isAgreements;
		const crsfToken = localStorage.getItem("_crsf"), tel = localStorage.getItem("userTel");
		if(tel && crsfToken){
			this.isNeedLogin = !1; 
			this.login2(tel, crsfToken);
		}
	}
});